/**
 * copyright (c) 2017-2023 厦门风领科技有限公司
 * https://www.fengling.cn/
 * 
 * 数值服务-用于访问游戏的所有数据
 * 
 * 2023-10-16 20:15:07
 */

import { sys } from "cc";
import { ValueType } from "../../enum/ValueType";
import { GGStore } from "../core/base/GGStore";

export type Value = {
    count: number;
    valueType: number;
}

const valueServices = {};

/** 数值服务 */
export class ValueService {

    public static EEventNames = {
        UPDATE: 'VALUE_SERVICE_UPDATE'
    };

    // 当前数值服务对应的游戏标识，用于区分合集中不同的游戏。
    private appId: string = 'frame';
    private storeKey: string = '';
    // 缓存数据
    private cacheData = null;

    /**
     * 获取单例对象
     * @param appId 游戏的标记 
     * @returns 
     */
    public static getInstance(appId?: string): ValueService {
        return valueServices[appId] || new ValueService(appId);
    }

    constructor(appId?: string) {
        this.appId = appId || gg.getAppId() || this.appId;
        this.storeKey = `${this.appId}_gameData`;

        valueServices[this.appId] = this;

        this.initData();
    }

    private initData() {
        if (this.cacheData) { return; }
        this.cacheData = GGStore.get(this.storeKey, {});
    }

    /**
     * 获取数值
     * @param valueType 数值枚举类型
     * @returns 
     */
    public getCount(valueType: ValueType): number {
        return this.cacheData[valueType];
    }

    /**
     * 更新数值
     * @param values 要更新的数值列表
     * @returns 
     */
    public update(values: Value[]) {
        if (!values?.length)
            return;

        let value: Value;
        for (let i = 0; i < values.length; i++) {
            value = values[i];

            value.valueType = Number(value.valueType);
            if (isNaN(value.valueType))
                continue;

            value.count = Number(value.count);
            if (isNaN(value.count))
                continue;

            this.cacheData[value.valueType] = value.count;
        }

        this.updateToSDKSendEvent(values);
        gg.systemEvent.emit(ValueService.EEventNames.UPDATE, values);
        GGStore.set(this.storeKey, this.cacheData);
    }

    /**
     * 更新数据，超休框架新增
     * @param valueType 数据枚举类型
     * @param count 数值
     */
    public updateData(valueType: ValueType, count: number) {
        return this.update([{ valueType, count }]);
    }


    public updateToSDKSendEvent(values: Value[]) {
        let key = '', value: Value, data = {};

        for (let i = 0; i < values.length; i++) {
            value = values[i];
            key = '';

            switch (value.valueType) {
                // 公共事件属性
                // case ValueType.gem: key = 'temp_gem'; break;
                // case ValueType.gole: key = 'temp_gold'; break;
                // case ValueType.points: key = 'temp_points'; break;
                // case ValueType.game_mode: key = 'game_mode'; break;
                // case ValueType.max_chapter_level: key = 'max_chapter_level'; break;
                // case ValueType.max_chapter: key = 'max_chapter'; break;
                // case ValueType.role_level: key = 'role_level'; break;

                // 用户数据
                case ValueType.current_level: key = 'current_level'; break;
                case ValueType.current_chapter: key = 'current_chapter'; break;
                case ValueType.current_chapter_level: key = 'current_chapter_level'; break;
                case ValueType.current_coin: key = 'current_coin'; break;
                case ValueType.current_gem: key = 'current_gem'; break;
                case ValueType.current_points: key = 'current_points'; break;
                case ValueType.current_key: key = 'current_key'; break;
                case ValueType.current_stamina: key = 'current_stamina'; break;
                case ValueType.current_full_ranking: key = 'current_full_ranking'; break;
                case ValueType.current_friends_ranking: key = 'current_friends_ranking'; break;
                case ValueType.total_ad_times: key = 'total_ad_times'; break;
                case ValueType.total_consume_coin: key = 'total_consume_coin'; break;
                case ValueType.total_gain_coin: key = 'total_gain_coin'; break;
                case ValueType.total_invite_times: key = 'total_invite_times'; break;
                case ValueType.total_share_num: key = 'total_share_num'; break;
                case ValueType.total_invite_num: key = 'total_invite_num'; break;
                case ValueType.total_challenge_cup_num: key = 'total_challenge_cup_num'; break;
                case ValueType.total_challenge_num: key = 'total_challenge_num'; break;
                case ValueType.total_login: key = 'total_login'; break;

                default: break;
            }

            key !== '' && (data[key] = value.count);
        }

        if (sys.platform == sys.Platform.WECHAT_GAME) {
            gg.sdk.sendEventUserSet(data);
        }
    }
}
